package com.jyxd.web.service.czform;

import com.jyxd.web.dao.czform.MoreForeUniversalDao;
import com.jyxd.web.data.czform.MoreForeUniversal;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class MoreForeUniversalService {

    @Autowired
    private MoreForeUniversalDao moreForeUniversalDao;

    public boolean insert(MoreForeUniversal moreForeUniversal) {
        return moreForeUniversalDao.insert(moreForeUniversal);
    }


    public MoreForeUniversal queryData(String id) {
        return moreForeUniversalDao.queryData(id);
    }


    public boolean update(MoreForeUniversal moreForeUniversal) {
        return moreForeUniversalDao.update(moreForeUniversal);
    }


    public HttpResult deleteData(String rowId) {
        if (StringUtils.isEmpty(rowId)) {
            return new HttpResult(200, "参数错误，删除失败");
        }
        moreForeUniversalDao.deleteData(rowId);
        return new HttpResult(200, "删除成功");
    }

    public List<MoreForeUniversal> queryList(Map<String, Object> map) {
        return moreForeUniversalDao.queryList(map);
    }


    public int queryNum(Map<String, Object> map) {
        return moreForeUniversalDao.queryNum(map);
    }

    public HttpResult getDataByByRowId(Map<String, Object> map) {
        if (CollectionUtils.isEmpty(map)) {
            return new HttpResult(200, "参数不能为空");
        }
        List<MoreForeUniversal> list = moreForeUniversalDao.queryList(map);
        list.forEach(moreForeUniversal -> {
            map.put(moreForeUniversal.getCode(), moreForeUniversal.getValue());
        });
        return new HttpResult(200, map, "查询成功");
    }

    public HttpResult getListByRowId(Map<String, Object> map) {
        if (Objects.isNull(map)) {
            return new HttpResult(200, "参数不能为空");
        }
        Integer totalCount = null;
        if (map.containsKey("start") && map.containsKey("size")) {
            totalCount = moreForeUniversalDao.getListByRowIdNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
        }
        List<MoreForeUniversal> list = moreForeUniversalDao.getListByRowId(map);
        if (CollectionUtils.isEmpty(list)) {
            return new HttpResult(200, "暂无数据");
        }
        return new HttpResult(200, list, totalCount, "查询成功");
    }

    /**
     * 新增或者修改患者风险评估报告
     *
     * @param map
     * @param userName
     * @return
     */
    public HttpResult saveOrUpdate(Map<String, Object> map, String userName) {
        if (CollectionUtils.isEmpty(map)) {
            return new HttpResult(200, "参数不能为空");
        }
        if (Objects.isNull(map.get("patientId"))) {
            return new HttpResult(200, "患者id不能为空");
        }
        if (Objects.isNull(map.get("type"))) {
            return new HttpResult(200, "类型不能为空");
        }
        //如果包含rowId则进行修改
        if (map.containsKey("rowId") && StringUtils.isNotEmpty((String) map.get("rowId"))) {
            return updateData(map, userName);
        }
        //否则进行新增操作
        return saveData(map, userName);
    }

    /**
     * 修改数据
     *
     * @param map
     * @param userName
     * @return
     */
    private HttpResult updateData(Map<String, Object> map, String userName) {
        String rowId = String.valueOf(map.get("rowId"));
        String patientId = String.valueOf(map.get("patientId"));
        String type = String.valueOf(map.get("type"));
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("rowId", rowId);
        queryMap.put("patientId", patientId);
        queryMap.put("type", type);
        List<MoreForeUniversal> list = queryList(queryMap);
        Date date = new Date();
        list.forEach(moreForeUniversal -> {
            //如果map中包含数据则进行修改，否则进行删除
            if (map.containsKey(moreForeUniversal.getCode())) {
                moreForeUniversal.setValue(String.valueOf(map.get(moreForeUniversal.getCode())));
                moreForeUniversal.setOperatorCode(userName);
                moreForeUniversal.setUpdateTime(date);
                update(moreForeUniversal);
                map.remove(moreForeUniversal.getCode());
            } else {
                moreForeUniversal.setStatus(-1);
                moreForeUniversal.setUpdateTime(date);
                update(moreForeUniversal);
            }
        });
        if (map.size() <= 3) {
            return new HttpResult(200, "修改成功");
        }
        //如果内容中有新增的数据则新增，但保持rowId不变
        setPatientScoreRecord(map, userName);
        return new HttpResult(200, "修改成功");
    }

    private HttpResult saveData(Map<String, Object> map, String userName) {
        String rowId = UUIDUtil.getUUID();
        map.put("rowId", rowId);
        setPatientScoreRecord(map, userName);
        return new HttpResult(200, "保存成功");
    }

    private void setPatientScoreRecord(Map<String, Object> map, String userName) {
        String patientId = String.valueOf(map.get("patientId"));
        String rowId = String.valueOf(map.get("rowId"));
        String type = String.valueOf(map.get("type"));
        Date date = new Date();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (StringUtils.equals(entry.getKey(), "patientId")) {
                continue;
            }
            if (StringUtils.equals(entry.getKey(), "rowId")) {
                continue;
            }
            if (StringUtils.equals(entry.getKey(), "type")) {
                continue;
            }
            /*if (StringUtils.isEmpty(String.valueOf(entry.getValue()))) {
                continue;
            }*/
            MoreForeUniversal moreForeUniversal = new MoreForeUniversal();
            moreForeUniversal.setId(UUIDUtil.getUUID());
            moreForeUniversal.setPatientId(patientId);
            moreForeUniversal.setRowId(rowId);
            moreForeUniversal.setCreateTime(date);
            moreForeUniversal.setUpdateTime(date);
            moreForeUniversal.setType(type);
            moreForeUniversal.setOperatorCode(userName);
            moreForeUniversal.setStatus(1);
            moreForeUniversal.setCode(entry.getKey());
            moreForeUniversal.setValue(String.valueOf(entry.getValue()));
            insert(moreForeUniversal);
        }
    }
}
